package com.itheima.Controller;

import com.itheima.Service.EmpService;
import com.itheima.pojo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/emps")
@Slf4j
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;
    @GetMapping

    public Result page(EmpQueryParam param) {
        log.info("请求参数： {}", param);
        PageBean pageBean = empService.page(param);
        return Result.success(pageBean);
    }
    @PostMapping
    public Result save(@RequestBody Emp emp) {
        log.info("新增部门:{}", emp);
        System.out.println("新增部门:" + emp);
        empService.save(emp);
        return Result.success();
    }
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        log.info("删除部门:{}", ids);
        empService.deleteByIds(ids);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据id查询员工的详细信息");
        Emp emp  = empService.getInfo(id);
        return Result.success(emp);
    }

    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("修改部门:{}", emp);
        empService.update(emp);

        return Result.success();
    }
    @RestControllerAdvice
    public class GlobalExceptionHandler {

        //处理异常
        @ExceptionHandler
        public Result ex(Exception e){//方法形参中指定能够处理的异常类型
            e.printStackTrace();//打印堆栈中的异常信息
            //捕获到异常之后，响应一个标准的Result
            return Result.error("对不起,操作失败,请联系管理员");
        }
    }
    @GetMapping("list")
    public Result list(){
        List<Emp> list = empService.list();
        return Result.success(list);
    }
}
